package com.wolfeyes.framework.web.filter;

import com.wolfeyes.framework.web.context.WebContextUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @title Servlet容器上下文环境的过滤器
 * @className WebContextFilter
 * @author yanyljava
 * @create 2022-01-14 21:25
 */
public class WebContextFilter implements Filter {

	/**
	 * @title 过滤器的初始化操作
	 */
	public void init(FilterConfig filterConfig) throws ServletException {
		
	}

	/**
	 * @title 过滤逻辑的核心操作方法
	 */
	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)servletRequest;
		HttpServletResponse response = (HttpServletResponse)servletResponse;
		//if (!request.getMethod().equalsIgnoreCase("OPTIONS")) {
			WebContextUtils.init(request, response);

			try {
				filterChain.doFilter(request, response);
			} finally {
				WebContextUtils.destroy();
			}

		//}
	}

	/**
	 * @title 过滤器的销毁操作
	 */
	@Override
	public void destroy() {
		
	}
	
}

